package com.hengxinguotong.zhihuichengjian.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.bean.MenuBean;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by liux on 15/9/10.
 */
public class MenuListAdapter extends BaseAdapter {
    private ViewHolder holder;
    private String TAG = "MenuListAdapter";
    private Context mContext;
    private List<MenuBean> menuBeanList;

    public MenuListAdapter(Context context, List<MenuBean> menuBeanList) {
        mContext = context;
        this.menuBeanList = menuBeanList;
    }

    public void setMenuBeanList(List<MenuBean> menuBeanList) {
        this.menuBeanList = menuBeanList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return menuBeanList.size();
    }

    @Override
    public Object getItem(int position) {
        return menuBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null || convertView.getTag() == null) {
            convertView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_menu_list, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        MenuBean menuBean = menuBeanList.get(position);
        holder.menuName.setText(menuBean.getName());
        int smallIcon = Utils.getSmallIconByUrl(menuBean.getId());
        if (smallIcon > 0) {
            holder.menuIcon.setImageResource(smallIcon);
        }
        int type = position%2;
        if(type == 0){
            holder.menuLl.setBackgroundResource(R.color.color_item_bg);
        }else if(type ==1){
            holder.menuLl.setBackgroundResource(R.color.white_ffffff);
        }
        if(menuBean.isHasNew()){
            holder.messageNew.setVisibility(View.VISIBLE);
        }else{
            holder.messageNew.setVisibility(View.GONE);
        }
        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.menu_icon)
        ImageView menuIcon;
        @BindView(R.id.menu_name)
        HXTextView menuName;
        @BindView(R.id.menu_ll)
        LinearLayout menuLl;
        @BindView(R.id.message_new)
        ImageView messageNew;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
